package com.jcrj.service;

import com.jcrj.dao.SysLogDao;
import com.jcrj.entity.OSysLog;
import com.jcrj.entity.OTaskReport;
import com.jcrj.entity.OUser;
import com.jcrj.util.DateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SysLogService {
	private SysLogDao sysLogDao;

	public SysLogDao getSysLogDao() {
		return this.sysLogDao;
	}

	public void setSysLogDao(SysLogDao sysLogDao) {
		this.sysLogDao = sysLogDao;
	}

	public void saveLog(String module, String userName, String content) {
		OSysLog log = new OSysLog();
		log.setModule(module);
		log.setContent(content);
		log.setUserName(userName);
		log.setTime(new Date());
		this.sysLogDao.saveLog(log);
	}

	public void deleteSysLogs(String[] ids) {
		if (ids != null) {
			String id = "";
			for (int i = 0; i < ids.length; i++) {
				if (id == "")
					id = id + ids[i];
				else {
					id = id + "," + ids[i];
				}
			}
			String hql = "delete from OSysLog where id in(" + id + ")";
			this.sysLogDao.executeHql(hql);
		}
	}

	public void deleteAllSysLogs(String user, String module, String startDate, String endDate) {
		String hql = "delete from  OSysLog where 1=1 ";
		if ((user != null) && (!"".equals(user))) {
			hql = hql + " and  userName like'%" + user.trim() + "%'";
		}
		if ((module != null) && (!"".equals(module))) {
			hql = hql + " and  module like'%" + module.trim() + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and time >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and time <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		this.sysLogDao.executeHql(hql);
	}

	public Long countSysLog(String user, String module, String startDate, String endDate) {
		String hql = "select count(id) from OSysLog where 1=1 ";
		if ((user != null) && (!"".equals(user))) {
			hql = hql + " and  userName like'%" + user.trim() + "%'";
		}
		if ((module != null) && (!"".equals(module))) {
			hql = hql + " and  module like'%" + module.trim() + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and time >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and time <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		return this.sysLogDao.countTotalByHql(hql);
	}

	public List<OSysLog> LogListManager(String user, String module, String startDate, String endDate, int currentPage) {
		String hql = "from OSysLog where 1=1";
		if ((user != null) && (!"".equals(user))) {
			hql = hql + " and userName like'%" + user.trim() + "%'";
		}
		if ((module != null) && (!"".equals(module))) {
			hql = hql + " and  module like'%" + module.trim() + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and time >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and time <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		hql = hql + "order by time desc";
		return this.sysLogDao.findByPage(hql, currentPage);
	}

	public List<Integer> getFlowAgentUser(OUser user) {
		String nowDate = DateUtil.DateToString(new Date());
		String hql = " from OTaskReport t where selectUserId =" + user.getId() + " and startDate<=to_date('" + nowDate + "','yyyy-mm-dd') and to_date('"
				+ nowDate + "','yyyy-mm-dd')<=endDate";
		List<OTaskReport> list = this.sysLogDao.findByHql(hql);
		List userList = new ArrayList();
		if ((list != null) && (!list.isEmpty())) {
			for (OTaskReport report : list) {
				userList.add(report.getUserId());
			}
		}
		return userList;
	}

	public String getAgentUser(OUser user) {
		String nowDate = DateUtil.DateToString(new Date());
		String users = user.getId().toString();
		String hql = " from OTaskReport t where selectUserId =" + user.getId() + " and startDate<=to_date('" + nowDate + "','yyyy-mm-dd') and to_date('"
				+ nowDate + "','yyyy-mm-dd')<=endDate";
		List<OTaskReport> list = this.sysLogDao.findByHql(hql);
		if ((list != null) && (!list.isEmpty())) {
			for (OTaskReport report : list) {
				users = users + "," + report.getUserId();
			}
		}
		return users;
	}
}

/*
 * Location: /Volumes/work/oa/WEB-INF/classes/ Qualified Name:
 * com.jcrj.service.SysLogService JD-Core Version: 0.6.0
 */